import { query } from "../mysqlQuery.js";
import Joi from "joi";
import SDTime from "silly-datetime"; //时间格式化处理插件
import {getRandomId} from "../common/index.js"
//查
function queryUserTask(req, res, next) {
  query([
    {
      sql: "select * from userdowntasktable where user_id=?",
      values: [req.auth.user_id],
    },
  ])
    .then((out) => {
      res.send({
        code: 200,
        data: out[0].rows,
      });
    })
    .catch((err) => {
      next(err);
    });
}
//增
function addUserTask(req, res, next) {
  const schema = Joi.object().keys({
    fileNameId: Joi.string().required(),
    bookName: Joi.string().required(),
  });
  const result = Joi.validate(req.body, schema, { allowUnknown: true });
  console.log(result);
  if (!result.error) {
    let task_id=getRandomId(8)
    query([
      {
        sql: "insert into userdowntasktable (user_id,fileNameId,bookName,task_id) values (?,?,?,?)",
        values: [req.auth.user_id, req.body.fileNameId, req.body.bookName,task_id],
      },
    ])
      .then((out) => {
        res.send({
          code: 200,
          data:Object.assign({},out[0].rows,{task_id}),
        });
      })
      .catch((err) => {
        next(err);
      });
  } else {
    next(result.error);
  }
}
//删
function loseUserTask(req, res, next) {
  const schema = Joi.object().keys({
    ids: Joi.string().required(),
  });

  const result = Joi.validate(req.body, schema, { allowUnknown: true });

  if (!result.error) {
    try {
      let test = req.body.ids.split(","); //检测格式是否正确
      query([
        {
          sql: `delete from userdowntasktable where id in (${ids})`,
          values: [],
        },
      ])
        .then((out) => {
          res.send({
            code: 200,
            data: out[0],
          });
        })
        .catch((err) => {
          next(err);
        });
    } catch (error) {
      next(error);
    }
  } else {
    next(result.error);
  }
}
//改是否下载成功
function putUserTaskisSuccess(req, res, next) {
  const schema = Joi.object().keys({
    task_id: Joi.number().required(),
    downStatus: Joi.number().required(),
  });
  const result = Joi.validate(req.body, schema, { allowUnknown: true });
  if (!result.error) {
    query([
      {
        sql: "update userdowntasktable set downStatus=? where task_id=?;",
        values: [req.body.downStatus, req.body.task_id],
      },
    ])
      .then((out) => {
        res.send({
          code: 200,
          data: out[0],
        });
      })
      .catch((err) => {
        next(err);
      });
  } else {

    next(result.error);
  }
}
//改下载时间
function putUserTaskDownloadTime(req, res, next) {
  const schema = Joi.object().keys({
    task_id: Joi.string().required(),
  });
  const result = Joi.validate(req.body, schema, { allowUnknown: true });
  console.log(result,"----")
  if (!result.error) {
    query([
      {
        sql: "update userdowntasktable set download_time=? where task_id=?;",
        values: [SDTime.format(new Date(), "YYYY-MM-DD HH:mm:ss"), req.body.task_id],
      },
    ])
      .then((out) => {
        res.send({
          code: 200,
          data: out[0],
        });
      })
      .catch((err) => {
        next(err);
      });
  } else {
    next(result.error);
  }
}
export {
  loseUserTask,
  addUserTask,
  putUserTaskDownloadTime,
  putUserTaskisSuccess,
  queryUserTask
};
